package com.xneure.utils.cns;

import java.util.HashMap;
import java.util.Map;

import com.xneure.utils.ObjectUtil;

public class CharFilter {
	private char _char;
	private Map<Character, CharFilter> ct;

	public CharFilter(char[] c, int index) {
		append(c, index);
	}

	public CharFilter(String str) {
		this(str.toCharArray(), 0);
	}

	public void append(char[] c, int index) {
		CharFilter cf = null;
		if (null == ct) {
			ct = new HashMap<Character, CharFilter>();
		}
		if (_char == c[index++]) {
			cf = ct.get(c[index]);
		} else {
			_char = c[index - 1];
		}
		if (index < c.length) {
			if (null == cf) {
				ct.put(c[index], new CharFilter(c, index));
			} else {
				cf.append(c, index);
			}
		}
	}

	public char getChar() {
		return _char;
	}

	public boolean verify(String str) {
		return verify(str.toCharArray(), 0);
	}

	public boolean verify(char[] c, int index) {
		if (_char == c[index++]) {
			if (index == c.length) {
				return ObjectUtil.isNull(ct);
			} else {
				CharFilter charFilter = ct.get(c[index]);
				return ObjectUtil.isNull(charFilter) ? false : charFilter.verify(c, index);
			}
		} else {
			return false;
		}
	}

	public void append(String str) {
		append(str.toCharArray(), 0);
	}
}
